//
//  VideoCell.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/10/31.
//

#import "VideoCell.h"
#import <UIButton+WebCache.h>

@interface VideoCell ()

@property (nonatomic, strong) UIButton *videoBtn;
@property (nonatomic, strong) UIButton *playIcon;
@property (nonatomic, strong) UIButton *deleBtn;
@end

@implementation VideoCell

- (void)initViews {
    self.backgroundColor = [UIColor clearColor];
    [self addSubview:self.videoBtn];
    [self addSubview:self.playIcon];
    [self addSubview:self.deleBtn];
    
    [self.videoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [self.playIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.width.height.mas_equalTo(30);
    }];
    [self.deleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-4);
        make.top.mas_equalTo(4);
        make.width.height.mas_equalTo(20);
    }];
}

- (void)setImage:(UIImage *)image {
    _image = image;
    [self.videoBtn setImage:image forState:UIControlStateNormal];
    self.playIcon.hidden = NO;
    self.deleBtn.hidden = NO;
}

- (void)setImageUrl:(NSString *)imageUrl {
    _imageUrl = imageUrl;
    [self.videoBtn sd_setImageWithURL:[NSURL URLWithString:imageUrl] forState:UIControlStateNormal];
    self.playIcon.hidden = NO;
    self.deleBtn.hidden = NO;
}

- (UIButton *)videoBtn {
    if (!_videoBtn) {
        _videoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_videoBtn setBackgroundImage:[UIImage imageNamed:@"uploadVideo"] forState:UIControlStateNormal];
        @weakify(self)
        [[_videoBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.playIcon.hidden) {
                if (self.showVideoPickerBlcok) {
                    self.showVideoPickerBlcok();
                }
            }else {
                if (self.playVideoBlock) {
                    self.playVideoBlock();
                }
            }
        }];
    }
    return _videoBtn;
}

- (UIButton *)playIcon {
    if (!_playIcon) {
        _playIcon = [UIButton buttonWithType:UIButtonTypeCustom];
        [_playIcon setImage:[UIImage imageNamed:@"icon_play"] forState:UIControlStateNormal];
        _playIcon.hidden = YES;
        @weakify(self)
        [[_playIcon rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.playVideoBlock) {
                self.playVideoBlock();
            }
        }];
    }
    return _playIcon;
}

- (UIButton *)deleBtn {
    if (!_deleBtn) {
        _deleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleBtn setImage:[UIImage imageNamed:@"icon_del"] forState:UIControlStateNormal];
        _deleBtn.hidden = YES;
        @weakify(self)
        [[_deleBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            self.deleBtn.hidden = self.playIcon.hidden = YES;
            if (self.deleteVideoBlock) {
                self.deleteVideoBlock();
            }
        }];
    }
    return _deleBtn;
}

@end
